{% extends "index.twig" %}
{% block content %}

    {% set tstring = url_param('q','string') %}
    {% set length = tag_count[tstring] %}
    {% if length == 1 %}
        <h4>1 post is tagged "{{tstring}}"</h4>
    {% else %}
        <h4>{{ length }} posts are tagged "{{tstring}}"</h4>
    {% endif %}

    {% if config.tagblog.paging > 0 and length > config.tagblog.paging %}

        {% set p = url_param('p','int',1) %}
        {% set offset = (p - 1) * config.tagblog.paging %}
        {% set lastpage = length//config.tagblog.paging %}
        {% if length%config.tagblog.paging > 0 %}{% set lastpage = lastpage + 1 %}{% endif %}

        {% for page_id in tag_pages[tstring]|slice(1)|split('#')|slice(offset,config.tagblog.paging) %}
            {% set page = pages[page_id] %}
            {% include 'includes/postlistitem.twig' %}
        {% endfor %}
        <div class="pagination clearfix">
            {% if offset >= config.tagblog.paging %}
                <div class="previous"><a href="/tags?q={{tstring}}&p={{ p - 1 }}">&lt;Previous Page</a></div>
            {% endif %}
            {# {% if offset >= config.tagblog.paging and offset + config.tagblog.paging < length %} | {% endif %} #}
            <div class="current">- {{ p }}/{{ lastpage }} -</div>
            {% if offset + config.tagblog.paging < length %}
                <div class="next"><a href="/tags?q={{tstring}}&p={{ p + 1 }}">Next Page&gt;</a></div>
            {% endif %}
        </div>
    {% else %}
        {% for page_id in tag_pages[tstring]|slice(1)|split('#') %}
            {% set page = pages[page_id] %}
            {% include 'includes/postlistitem.twig' %}
        {% endfor %}
    {% endif %}

{% endblock content %}
